<style lang="scss" scoped>
.custom-drag {
  height: 100%;
  width: 100%;
  overflow: auto;
  transition: all 1s;
  // & > div {
  //   width: 100%;
  //   height: 100%;
  //   span {
  //     display: flex;
  //     width: 100%;
  //   }
  // }
}
.drag-item {
  padding: 5px;
  border: 1px dotted #2f3e59;
  cursor: move;
  position: relative;
  &:hover {
    border: 1px dotted #fff;
    background-color: #2f3e59;
    .delete-icon {
      opacity: 1;
    }
  }
  .delete-icon {
    position: absolute;
    opacity: 0;
    right: 0;
    bottom: 0;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    font-size: 5px;
    padding: 2px 5px;
    z-index: 999;
    background-color: #2f3e59;
    user-select: none;
    .icon {
      padding: 0 5px;
      &:hover {
        color: #0091ff;
      }
    }
  }
}

.active-drag {
  border: 1px dotted #fff;
  background-color: #2f3e59;
}

.ghost {
  background-color: #0091ff;
}
.chosen {
  background-color: #0091ff;
}
</style>

<template>
  <div class="custom-drag" :drag-type="slotKey">
    <draggable
      v-model="dragData"
      chosenClass="chosen"
      ghostClass="ghost"
      forceFallback="true"
      group="people"
      animation="1000"
      @start="onStart"
      @end="onEnd"
      :move="onMove"
    >
      <transition-group :style="areaData.style" :drag-type="slotKey">
        <div
          class="drag-item"
          @click="onClickDragItem(item)"
          :class="{ 'active-drag': editDragId == item.id }"
          :style="{ width: `${item.style.width}`, height: `${'auto'}` }"
          v-for="(item, index) in areaData.data"
          :key="item.id"
          :id="item.id"
        >
          <!-- INput -->
          <Input
            v-model="item.props.value"
            :readonly="true"
            :placeholder="item.props.placeHolderValue + item.id"
            v-if="item.code == 'Input'"
            :style="{ width: `calc(${item.style.width} - 20px)` }"
          />
          <!-- TABLE -->
          <Table
            v-else-if="item.code == 'Table'"
            :columns="item.props.columns"
            :data="item.props.data"
          ></Table>

          <Button
            v-else-if="item.code == 'Button'"
            :ghost="item.props.ghost"
            :type="item.props.type"
            >Primary</Button
          >

          <div class="delete-icon">
            <div class="iconfont icon-shangyibu icon" v-if="index != 0" title="上移/左移"></div>
            <span v-if="index != 0">|</span>
            <div
              class="iconfont icon-xiayibu icon"
              v-if="index != areaData.data.length - 1"
              title="下移/右移"
            ></div>
            <span v-if="index != areaData.data.length - 1">|</span>
            <div class="iconfont icon-guanbi icon" @click="deleteComp(item.id)"></div>
          </div>
        </div>
      </transition-group>
    </draggable>
  </div>
</template>

<script>
import Draggable from "vuedraggable";
export default {
  name: "custom-drag",
  components: {
    Draggable,
  },
  props: {
    areaData: {
      type: Object,
      default: () => {},
    },
    slotKey: {
      type: String,
    },
    editDragId: {
      type: String,
      default: "",
    },
  },
  watch: {
    areaData: {
      handler(val) {
        console.log(`监听到${this.slotKey}模块数据发生变化`, val);
      },
      deep: true,
    },
  },
  computed: {
    dragData: {
      get() {
        return this.areaData.data;
      },
      set(e) {
        const newDragData = this.areaData;
        newDragData.data = e;
        this.$emit("update:areaData", newDragData);
      },
    },
  },
  methods: {
    onClickDragItem(dragItem) {
      this.$emit("onClickDrag", {
        dragItem,
        slotKey: this.slotKey,
      });
    },
    onStart(e) {
      console.log("开始拖拽", e);
    },
    onEnd(e) {
      console.log("结束拖拽", e);
    },
    onMove(e, originalEvent) {
      console.log(e);
      console.log(originalEvent);
      // //不允许停靠
      // if (e.relatedContext.element.id == 1) return false;
      // //不允许拖拽
      // if (e.draggedContext.element.id == 4) return false;
      return true;
    },

    /**
     * 删除组件
     * @param {*} id
     */
    deleteComp(id) {
      this.$emit("onDeleteComp", this.slotKey, id);
    },
  },
};
</script>
